package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadLetterMqConfig {
    // 声明一些变量
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";


    /**
     * 声明交换机/死信交换机
     *
     * @return
     */
    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(exchange_dead, true, false);
    }

    /**
     * 声明普通队列
     *
     * @return
     */
    @Bean
    public Queue queue1() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", exchange_dead);
        args.put("x-dead-letter-routing-key", routing_dead_2);
        // 设置延迟时间  -- 设置延迟时间为10S 进入到该队列中消息10s后被投递到死信交换器
        args.put("x-message-ttl", 10 * 1000);
        //参数1：队列名称 参数2：是否持久化 参数3：是否排他 true:只允许创建该队列应用使用该队列 参数4：是否自动删除 参数5：设置队列特征 设置
        return new Queue(queue_dead_1, true, true, false, args);
    }


    /**
     * 声明死信队列-接收是死信消息
     * @return
     */
    @Bean
    public Queue queue2(){
        return new Queue(queue_dead_2, true, true, false);
    }


    //设置队列1的绑定规则
    @Bean
    public Binding binding1(){
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1);
    }


    //设置队列二的绑定规则
    @Bean
    public Binding binding2(){
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }
}
